from fastapi import APIRouter, Depends, HTTPException
from typing import Dict, Any
from mooc.utils.wechat_client import WeChatAPI

router = APIRouter()
wechat_client = WeChatAPI()

@router.get("/access_token")
async def get_access_token() -> Dict[str, Any]:
    """获取微信access token"""
    try:
        access_token = await wechat_client.get_access_token()
        return {"access_token": access_token}
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

@router.post("/code2session")
async def code_to_session(code: str) -> Dict[str, Any]:
    """小程序登录"""
    try:
        result = await wechat_client.code2session(code)
        return result
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

@router.post("/qrcode")
async def generate_qrcode(scene: str) -> bytes:
    """生成小程序码"""
    try:
        qr_code = await wechat_client.get_unlimited_qrcode(scene)
        return qr_code
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))

@router.post("/send_template")
async def send_template_message(data: Dict[str, Any]) -> Dict[str, Any]:
    """发送订阅消息"""
    try:
        result = await wechat_client.send_template_message(data)
        return result
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e)) 